<?php

// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------


namespace app\common\dao\system\merchant;


use app\common\dao\BaseDao;
use app\common\model\BaseModel;
use app\common\model\system\merchant\MerchantCategory;
use app\common\model\system\merchant\MerchantCategoryIntention;
use crmeb\services\VicWordService;
use think\db\BaseQuery;
use think\facade\Db;


class MerchantCategoryIntentionDao extends BaseDao
{

    /**
     * @return string
     * @author: fupeng
     * @day: 2023/10/16
     */
    protected function getModel(): string
    {
        return MerchantCategoryIntention::class;
    }


    /**
     * @param array $where
     * @return BaseQuery
     * @author: fupeng
     * @day: 2023/10/16
     */
    public function search(?int $merId, array $where = [],$is_del =0)
    {
        $query =  MerchantCategoryIntention::getDB()->alias('ci')

            ->join('Merchant m','m.mer_id = ci.mer_id', 'left')
            ->when(($merId !== null && $merId != 0), function ($query) use ($merId) {
                $query->where('ci.mer_id', $merId);
            })
            ->when($is_del !== null, function ($query) use ($is_del) {
            $query->where('ci.is_del', $is_del);
        })
            ->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
                getModelTime($query, $where['date']);
            })
            ->when(isset($where['mer_category_intention_id']) && $where['mer_category_intention_id'] !== '', function ($query) use ($where) {
                    $query->where('mer_category_intention_id', $where['mer_category_intention_id']);
            });

        if (isset($where['keyword']) && $where['keyword']) {
            if (is_numeric($where['keyword'])) {
                $query->whereLike('mer_name|mer_keyword|mer_phone', "%{$where['keyword']}%");
            } else {
                $word = app()->make(VicWordService::class)->getWord($where['keyword']);
                $query->where(function ($query) use ($word, $where) {
                    foreach ($word as $item) {
                        if(mb_strlen($item) > 1) {
                            $query->whereOr('mer_name', 'LIKE', "%$item%");
                        }
                    }
                    $query->whereOr('mer_name|mer_keyword', 'LIKE', "%{$where['keyword']}%");
                });
            }
        }

        if(($merId == null || $merId == 0) && isset($where['status'])){
            if($where['status'] == '-1'){
                  $query->where('ci.status','not in', [3]);
            }else{
                $query->where('ci.status',$where['status']);
            }
        }else if (isset($where['status']) && $where['status'] != '-1'){
            if($where['status'] == '0'){
                $query->where('ci.status','in', [0,3]);
            }else{
                $query->where('ci.status',$where['status']);
            }
        }
        return $query;

    }

    /**
     * @return array
     * @author: fupeng
     * @day: 2023/10/16
     */
    public function allOptions()
    {
        $data = MerchantCategoryIntention::getDB()->column('category_name', 'merchant_category_id');
        $options = [];
        foreach ($data as $value => $label) {
            $options[] = compact('value', 'label');
        }
        return $options;
    }

    public function dateMerchantPriceGroup($date, $limit = 4)
    {
        return MerchantCategoryIntention::getDB()->alias('A')->leftJoin('Merchant B', 'A.merchant_category_id = B.category_id')
            ->leftJoin('StoreOrder C', 'C.mer_id = B.mer_id')->field(Db::raw('sum(C.pay_price) as pay_price,A.category_name'))
            ->when($date, function ($query, $date) {
                getModelTime($query, $date, 'C.pay_time');
            })->group('A.merchant_category_id')->where('pay_price', '>', 0)->order('pay_price DESC')->limit($limit)->select();
    }

    public function names(array $ids)
    {
        return MerchantCategoryIntention::getDB()->whereIn('merchant_category_id', $ids)->column('category_name');
    }
}
